package com.example.hello;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;

import java.text.SimpleDateFormat;
import java.util.Date;

public class message extends AppCompatActivity {

    private static final String TAG = "message";
    EditText name,height,weight,age;
    Spinner sex;
    String  str;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        name = (EditText) findViewById(R.id.name);
        height = (EditText) findViewById(R.id.height);
        weight = (EditText) findViewById(R.id. weight);
        age = (EditText) findViewById(R.id.age);
        sex = (Spinner) findViewById(R.id.sex);
        str = (String) sex.getSelectedItem();
        sex.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {

                //拿到被选择项的值
                str = (String) sex.getSelectedItem();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });
    }
    public void openOne(View btn) {
        String new_name = name.getText().toString();

        float new_height = Float.parseFloat(height.getText().toString());
        float new_age = Float.parseFloat(age.getText().toString());
        float new_weight = Float.parseFloat(weight.getText().toString());
        Date date = new Date();
        SimpleDateFormat DF = new SimpleDateFormat("dd-MM-yyyy");//获取本次访问时间
        SharedPreferences sharedPreferences = getSharedPreferences("message", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("name_key", new_name);
        editor.putFloat("age_key", new_age);
        editor.putFloat("height_key", new_height);
        editor.putFloat("weight_key", new_weight);
        editor.putString("sex_key", str);
        editor.putString("time",DF.format(date));
        //保存
        editor.commit();
        Intent config = new Intent(this, MainActivity.class);

        startActivity(config);//请求代码

        Log.i("open", "openOne: ");

    }
}